---
import { BaseLayout } from '../layouts';
import { LoginContent } from '../components/sections';
import {
  SITE_BASE_URL,
  VIEWPORT_NO_ZOOM,
  LOGIN_PAGE_PATH,
  PAGE_TITLES,
  LOGIN_DESCRIPTION,
  HOME_PAGE_PATH, // path to redirect authenticated users
  LOGIN_STATE_KEY // localStorage key indicating login state
} from '../constants';
import '../styles/components/login.css';
import { getCurrentLang } from '../utils';

const langDefault = getCurrentLang();
const langKey = langDefault.startsWith('zh') ? 'zh' : 'en';
const titleZh = PAGE_TITLES.LOGIN_ZH;
const titleEn = PAGE_TITLES.LOGIN_EN;
const title = langKey === 'zh' ? titleZh : titleEn;
Astro.response.headers.set('Cache-Control', 'no-store');
// Inline script that prevents authenticated users from visiting the login page.
const redirectScript = `
(function () {
  try {
    const d = document.documentElement;
    // Determine login status via DOM flag or persisted state.
    const logged =
      d.dataset.loggedIn === 'true' ||
      localStorage.getItem('${LOGIN_STATE_KEY}');
    // Replace current history entry so the login page isn't retained.
    if (logged) window.location.replace('${HOME_PAGE_PATH}');
  } catch (e) {
    // Fail silently to avoid blocking page rendering if any errors occur.
  }
})();
`;
---

<BaseLayout
  lang={langDefault}
  titleZh={titleZh}
  titleEn={titleEn}
  title={title}
  description={LOGIN_DESCRIPTION}
  viewport={VIEWPORT_NO_ZOOM}
  canonical={`${SITE_BASE_URL}${LOGIN_PAGE_PATH}/`}
  metaRobots="noindex, nofollow"
>
  <Fragment slot="head">
    <meta name="referrer" content="same-origin" />
    <!-- Redirect authenticated users away from the login page -->
    <script is:inline set:html={redirectScript} />
  </Fragment>
  <LoginContent client:load />
</BaseLayout>
